home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / nullfile / nullfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-15  |  2.0 KB  |  92 lines

  1. /* 
  2.  * nullfile.c --
  3.  *
  4.  *    Creates a file full of zeros (null characters).
  5.  *
  6.  * Copyright 1989 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/admin/nullfile/RCS/nullfile.c,v 1.1 91/04/14 22:08:17 jhh Exp Locker: jhh $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. #define min(a,b) (((a) < (b)) ? (a) : (b) )
  24. #define BUFFER_SIZE 4096
  25.  
  26. static char     *progName;
  27.  
  28. void        Usage();
  29.  
  30. void
  31. main(argc, argv)
  32.     int     argc;
  33.     char    *argv[];
  34. {
  35.     char     *name;
  36.     int        count;
  37.     FILE    *fp;
  38.     static char buffer[BUFFER_SIZE];
  39.     int        bytesWritten;
  40.     int        bytesToWrite;
  41.  
  42.     progName = argv[0];
  43.     if (argc != 3) {
  44.     Usage();
  45.     }
  46.     name = argv[1];
  47.     if (sscanf(argv[2], " %d", &count) != 1) {
  48.     Usage();
  49.     }
  50.     if (count < 0) {
  51.     fprintf(stderr,"Count argument must be positive.\n");
  52.     Usage();
  53.     }
  54.     fp = fopen(name,"w+");
  55.     if (fp == NULL) {
  56.     fprintf(stderr,"Unable to open file %s.\n", name);
  57.     exit(1);
  58.     }
  59.     bzero(buffer, BUFFER_SIZE);
  60.     for (bytesWritten = 0; bytesWritten < count;) {
  61.     bytesToWrite = min(BUFFER_SIZE, count - bytesWritten);
  62.     fwrite(buffer, sizeof(char), bytesToWrite, fp);
  63.     bytesWritten += bytesToWrite;
  64.    }
  65.    fclose(fp);
  66.    exit(0);
  67. }
  68.  
  69.  
  70. /*
  71.  *----------------------------------------------------------------------
  72.  *
  73.  * Usage --
  74.  *
  75.  *    Prints out the help message.
  76.  *
  77.  * Results:
  78.  *    None.
  79.  *
  80.  * Side effects:
  81.  *    None.
  82.  *
  83.  *----------------------------------------------------------------------
  84.  */
  85.  
  86. void
  87. Usage()
  88. {
  89.     (void) fprintf(stderr,"Usage: %s file count\n",progName);
  90.     exit(1);
  91. }
  92.